package com.lyc.servlet.admin;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyc.dao.SportsmanInfoDAO;
import com.lyc.dao.imp.SportsmanInfoDAOImpl;
import com.lyc.dto.SportsmanDto;
import com.lyc.entity.SportsmanInfo;

@WebServlet("/admin/Sportsman-update.let")
public class SportsmanUpdateServlet extends HttpServlet {

	private SportsmanInfoDAO dao = new SportsmanInfoDAOImpl();

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.reset();
		response.setContentType("application/json;charset=UTF-8");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
//			List<SportsmanDto> list = dao.selectMap(0, 999);
			SportsmanInfo e = new SportsmanInfo();
			String code = request.getParameter("sportsmanId");
			if (code == null || "".equals(code)) {
				throw new ServletException("人员编号不能为空");
			}
			if (!code.matches("^\\d+$")) {
				throw new ServletException("人员编号必须是数字");
			}
			/*
			 * for (SportsmanDto item : list) if (item.getSportsmanId() ==
			 * Integer.valueOf(code)) e.setCountryName(item.getCountryName());
			 */
			e.setSportsmanId(Integer.parseInt(request.getParameter("sportsmanId")));
			e.setCountryCode(request.getParameter("countryCode"));
			e.setSportsmanName(request.getParameter("sportsmanName"));
			e.setSportsmanGender(Integer.parseInt(request.getParameter("sportsmanGender")));
			e.setSportsmanBrithdate(sdf.parse(request.getParameter("sportsmanBrithdate")));
			e.setSportsmanHeight(Integer.parseInt(request.getParameter("sportsmanHeight")));
			e.setSportsmanWeigth(new BigDecimal(request.getParameter("sportsmanWeigth")));
			e.setSportsmanUri(request.getParameter("sportsmanUri"));
			e.setSportsmanDesc(request.getParameter("sportsmanDesc"));

			try {
				dao.updateById(e);
				response.getWriter().print("{\"ok\":true}");
			} catch (Exception ex) {
				throw new ServletException("人员编号不能重复", ex);
			}
		} catch (Exception e) {
			response.getWriter().print("{\"ok\":false,\"msg\":\"" + e.getMessage() + "\"}");
		}
		response.flushBuffer();
	}
}
